package com.ss.android.common.load;

import java.lang.ref.SoftReference;
import java.util.HashMap;

public final class LRUWeakCache<K, V> {
    public final CacheStrategyNode<K, V> mHead;
    public final CacheStrategyNode<K, V> mTail;
    public HashMap<K, CacheStrategyNode<K, V>> mMap;
    private int mCapacity;

    public LRUWeakCache() {
        this(16);
    }

    public LRUWeakCache(int capacity) {
        this.mMap = new HashMap();
        if (capacity <= 1) {
            throw new IllegalArgumentException("capacity must be great than one");
        }

        this.mCapacity = capacity;
        this.mHead = new CacheStrategyNode();
        this.mTail = new CacheStrategyNode();
        this.mHead.prev = this.mTail;
        this.mTail.next = this.mHead;
    }

    private void detach(CacheStrategyNode<K, V> node) {
        node.next.prev = node.prev;
        node.prev.next = node.next;
        node.prev = null;
        node.next = null;
    }

    private void attach(CacheStrategyNode<K, V> pre, CacheStrategyNode<K, V> newNode) {
        newNode.next = pre;
        newNode.prev = pre.prev;
        newNode.prev.next = newNode;
        pre.prev = newNode;
    }

    public final V get(K arg5) {
        Object v1 = null;
        CacheStrategyNode<K, V> v0 = this.mMap.get(arg5);
        if (v0 == null) {
            return null;
        } else {
            V v = v0.ref.get();
            if (v == null) {
                this.mMap.remove(arg5);
                if (v0.next != null && v0.prev != null) {
                    detach(v0);
                }
                return null;
            } else {
                if (v0.next != null && v0.prev != null && ((CacheStrategyNode) v0).next != this.mHead) {
                    detach(v0);
                    attach(this.mHead, v0);
                }
                return v;
            }
        }
    }

    public final void shrink() {
        while (this.mMap.size() > 8) {
            if (this.mTail.next == this.mHead) {
                return;
            }
            this.mMap.remove(this.mTail.next.key);
            detach(this.mTail.next);
        }
    }

    public final void put(K key, V v) {
        if (key != null && v != null) {
            CacheStrategyNode<K,V> cacheStrategyNode = this.mMap.get(key);
            if (cacheStrategyNode != null) {
                if (v != cacheStrategyNode.ref.get()) {
                    cacheStrategyNode.ref = new SoftReference(v);
                }

                cacheStrategyNode.key = key;
                if (cacheStrategyNode.next == null) {
                    return;
                }

                if (cacheStrategyNode.prev == null) {
                    return;
                }

                if (cacheStrategyNode.next == this.mHead) {
                    return;
                }

                detach(cacheStrategyNode);
                attach(this.mHead, cacheStrategyNode);
            } else {
                CacheStrategyNode<K,V> node = new CacheStrategyNode<>();
                node.key = key;
                node.ref = new SoftReference(v);
                this.mMap.put(key, node);
                attach(this.mHead, node);
                if (this.mMap.size() <= this.mCapacity) {
                    return;
                }
                if (this.mTail.next == this.mHead) {
                    return;
                }
                this.mMap.remove(this.mTail.next.key);
                detach(this.mTail.next);
            }
        }
    }
}

